package com.example.exploitapplication;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.ContentObserver;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class otherproviderActivity extends AppCompatActivity implements View.OnClickListener {

    //这里的AUTHORITY就是我们在AndroidManifest.xml中配置的authorities，这里的authorities可以随便写
    private static final String AUTHORITY = "com.example.student";

    private static final Uri URI = Uri.parse("content://" + AUTHORITY + "/student");
    private ContentResolver contentResolver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider);
        initView();
        initData();
    }

    private void initData() {
        contentResolver = getContentResolver();
        //注册内容观察者(可选，用于监控数据改变情况)
//        contentResolver.registerContentObserver(URI, true, new MyContentObserver(null));
    }

    private void initView() {
        findViewById(R.id.zeng).setOnClickListener(this);
        findViewById(R.id.shan).setOnClickListener(this);
        findViewById(R.id.gai).setOnClickListener(this);
        findViewById(R.id.cha).setOnClickListener(this);
        findViewById(R.id.openfile).setOnClickListener(this);
        findViewById(R.id.call).setOnClickListener(this);
        findViewById(R.id.fileprovider).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("aaa", "aaaa");
        //通过内容解析者操作内容观察者
        switch (v.getId()) {
            case R.id.zeng:
                contentResolver.insert(URI, contentValues);
                break;
            case R.id.shan:
                contentResolver.delete(URI, null, null);
                break;
            case R.id.gai:
                contentResolver.update(URI, contentValues, null, null);
                break;
            case R.id.cha:
                contentResolver.query(URI, null, null, null, null);
                break;
            case R.id.openfile:
                readprovider();
                writeprovider();
                break;
            case R.id.call:
                Bundle bundle = new Bundle();
                contentResolver.call(URI,"ABC","DEF",bundle);
                break;
            case R.id.fileprovider:
                fileprovider();
                break;
        }

    }

    private void fileprovider() {
    }


    private void readprovider() {
        try {
            String str = "content://" + AUTHORITY+"/out";
            OutputStream out =  contentResolver.openOutputStream(Uri.parse(str));
            out.write(str.getBytes());
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void writeprovider() {
        try {
            String str = "content://" + AUTHORITY+"/input";
            InputStream is = contentResolver.openInputStream(Uri.parse(str));
            byte[] buffer = new byte[1024];
            int byteCount;
            while ((byteCount = is.read(buffer)) != -1) {
                Log.e("Rzx",new String(buffer));
            }
            is.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //定义内容观察者
    class MyContentObserver extends ContentObserver {


        public MyContentObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.d("mmm", "改变了");
        }
    }
}